# delimit ;

use "$bpath/onesample.dta";

do metacode;
do bootstrap;

/* /\* *********FIGURES******** *\/ */

/* FIGURE 1: FOREST PLOTS */
admetan beta se_beta, by(nonlab) sortby(beta) re(eb) study(authors) forestplot( boxscale(0) range(-1.25 1.25) textscale(91) graphregion(color(white)));

graph export "forest.eps", replace;


/* ***********TABLES********* */

/* TABLE S1: Papers + Descriptions */
clear;
insheet using "$bpath/papers.csv", comma names;

listtab authors year identification using "Table_S1.tex", replace
type rstyle(tabular)
head("\begin{landscape}\begin{tabular}{rrr}""
\textit{Authors}&\textit{Year}&\textit{Identification}\\\\")
foot("\end{tabular}\end{landscape}");

/* TABLE 1: SUMMARY STATS, unweighted */
clear;
use "$bpath/onesample.dta";
sutex2 beta pval nobs lab pub fw ipr gpr comps subs highskill rival, varlabels replace saving("Table_1.tex");

clear;
use "$bpath/manysample.dta";
sutex2 beta pval nobs lab pub fw ipr gpr comps subs highskill rival, varlabels append saving("Table_1.tex");

/* TABLE 2: Effect size with standard error by lab and non-lab along with the p-value for the difference  */
clear;
use "$bpath/onesample.dta";
sum beta [aweight=wstar], meanonly;
local mean_ov = r(mean);
summ wstar, meanonly;
local sd_ov = sqrt( 1/r(sum) );

sum beta [aweight=wstar] if lab, meanonly;
local mean_lab = r(mean);
scalar lcount = r(N);
summ wstar if lab, meanonly;
local sd_lab = sqrt( 1/r(sum) );

sum beta [aweight=wstar] if ~lab, meanonly;
local mean_nonlab = r(mean);
scalar ncount = r(N);
summ wstar if ~lab, meanonly;
local sd_nonlab = sqrt( 1/r(sum) );

local Zdiff = abs(`mean_lab' - `mean_nonlab')/sqrt(`sd_lab'^2 + `sd_nonlab'^2);
local pdiff = 2*(1 - normal(`Zdiff') );

file open table2 using "$bpath/Table_2.tex", replace write;


file write table2
"\begin{tabular}{lcccc}"_newline
" &Overall&Lab&Non-Lab&p-value\\\\"_newline
"Summary Effect & " (round(`mean_ov',0.001)) " & " (round(`mean_lab',0.001)) " & " (round(`mean_nonlab',0.001)) " & " (round(`pdiff',0.001)) " \tabularnewline"_newline
" & (" (round(`sd_ov',0.001)) ") & (" (round(`sd_lab',0.001)) ") & (" (round(`sd_nonlab',0.001)) ") & \tabularnewline" _newline
"$ \tau^{2}$ & " (round(tau2,0.001)) " & " (round(ltau2,0.001)) " & " (round(ntau2,0.001)) " & " (round(ptau,0.001)) " \tabularnewline" _newline
"Observations & " (lcount + ncount) " & " (lcount) " & " (ncount) " &  \tabularnewline" _newline
"\end{tabular}"
;
file close table2;

/* TABLE S2: Dropping different measures */
/* full sample n=35*/
scalar full_sum = `mean_ov';
scalar full_se = `sd_ov';
scalar full_n = lcount + ncount;

quietly metareg beta lab, wsse(se_beta) eb;
scalar full_tau2=tau2;
matrix b = e(b);
matrix V = e(V);
scalar full_dif = b[1,1];
scalar full_difse = sqrt(V[1,1]);

/* elasticity only, n=32 */
drop if no_elast;
replace beta = belast;
replace se_beta = selast;
do metacode;
sum beta [aweight=wstar], meanonly;
scalar elast_sum = r(mean);
scalar elast_n = r(N);
summ wstar, meanonly;
scalar elast_se = sqrt( 1/r(sum) );

quietly metareg beta lab, wsse(se_beta) eb;
scalar elast_tau2=tau2;
matrix b = e(b);
matrix V = e(V);
scalar elast_dif = b[1,1];
scalar elast_difse = sqrt(V[1,1]);

/* same measure only, n=30 */
drop if diffdep;
do metacode;
sum beta [aweight=wstar], meanonly;
scalar sam_sum = r(mean);
scalar sam_n = r(N);
summ wstar, meanonly;
scalar sam_se = sqrt( 1/r(sum) );

quietly metareg beta lab, wsse(se_beta) eb;
scalar sam_tau2=tau2;
matrix b = e(b);
matrix V = e(V);
scalar sam_dif = b[1,1];
scalar sam_difse = sqrt(V[1,1]);

/* levels-levels only, n=23 */
drop if loglog;
do metacode;
sum beta [aweight=wstar], meanonly;
scalar lev_sum = r(mean);
scalar lev_n = r(N);
summ wstar, meanonly;
scalar lev_se = sqrt( 1/r(sum) );

quietly metareg beta lab, wsse(se_beta) eb;
scalar lev_tau2=tau2;
matrix b = e(b);
matrix V = e(V);
scalar lev_dif = b[1,1];
scalar lev_difse = sqrt(V[1,1]);

file open tableS2 using "$bpath/Table_S2.tex", replace write;

file write tableS2
"\begin{tabular}{lcccc}"_newline
" &Full Sample&Same Measure&Elasticities&Levels Only\\\\"_newline
"Summary Effect & " (round(full_sum,0.001)) " & " (round(sam_sum,0.001)) " & " (round(elast_sum,0.001)) " & " (round(lev_sum,0.001)) " \tabularnewline"_newline
" & (" (round(full_se,0.001)) ") & (" (round(sam_se,0.001)) ") & (" (round(elast_se,0.001)) ") & (" (round(lev_se,0.001)) ") \tabularnewline" _newline
"$ \tau^{2}$ & " (round(full_tau2,0.001)) " & " (round(sam_tau2,0.001)) " & " (round(elast_tau2,0.001)) " & " (round(lev_tau2,0.001)) " \tabularnewline" _newline
"$ \hat{\gamma}_{lab}-\hat{\gamma}_{field}$ & " (round(full_dif,0.001)) " & " (round(sam_dif,0.001)) " & " (round(elast_dif,0.001)) " & " (round(lev_dif,0.001)) " \tabularnewline" _newline
"SE($ \hat{\gamma}_{lab}-\hat{\gamma}_{field}$) & " (round(full_difse,0.001)) " & " (round(sam_difse,0.001)) " & " (round(elast_difse,0.001)) " & " (round(lev_difse,0.001)) " \tabularnewline" _newline
"Observations & " (full_n) " & " (sam_n) " & " (elast_n) " & " (lev_n) " \tabularnewline" _newline
"\end{tabular}"
;
file close tableS2;


/* /\* TABLE 3: Regression specifications *\/ */
clear;
use "$bpath/manysample.dta";

gen gprlab = gpr*lab;
gen iprlab = ipr*lab;
gen rivallab = rival*lab;
gen highlab = highskill*lab;
gen sublab = subs*lab;
gen fixlab = fw*lab;

do make_labels;

eststo, title("1"): metareg beta lab, wsse(se_beta) eb;
eststo, title("2"): metareg beta lab gpr rival highskill pub subs comps, wsse(se_beta) eb;
eststo, title("3"): metareg beta lab fw gpr rival highskill pub subs comps, wsse(se_beta) eb;
eststo, title("4"): metareg beta lab fw gpr rival highskill pub subs comps gprlab fixlab sublab, wsse(se_beta) eb;

/* generates .tex file */
esttab using "$bpath/Table_3.tex", b(3) se label star(* 0.10 ** 0.05 *** 0.01) order(lab gpr fw pub highskill subs comps rival) replace;
